
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Interface;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
using Spinning.Spinning.Entity;

namespace Spinning.Spinning.Unitity
{
    public class TouchManager:IUpdateHandler
    {
        List<ITouchable> _items = new List<ITouchable>();
        //TouchCollection _current = TouchPanel.GetState();
       // TouchCollection _previous = TouchPanel.GetState();
        BaseScene _scene;

        public TouchManager(BaseScene scene) {
            this._scene = scene;
        }

        public void RegisteTouchArea(ITouchable item)
        {
            _items.Add(item);
        }

        public void UnRegisteTouchArea(ITouchable item)
        {
            _items.Remove(item);
        }

        public void OnUpdate(GameTime gameTime)
        {
            TouchCollection _current = Game1.TouchCollection;

            foreach (TouchLocation tl in _current)
            {
                for (int i = 0; i < _items.Count; i++)
                {
                    ITouchable t = _items[i];

                    Vector2 pos = tl.Position;

                    if (t.Contain(pos))
                    {
                        if (tl.State == TouchLocationState.Pressed)//actions for began phase                   
                            t.OnTouch(tl);
                        if (tl.State == TouchLocationState.Moved)//actions for moved phase                   
                            t.OnHold(tl);
                        if (tl.State == TouchLocationState.Released)//actions for ended phase                    
                            t.OnRelease(tl);

                    }
                }
            }

            //_previous = _current;
        }

        //private bool isTouch(ITouchable t, TouchCollection tc)
        //{
        //    bool v = false;
        //    foreach (TouchLocation tl in tc)
        //    {
        //        if (t.Contain(tl.Position))
        //            v = true;
        //    }
        //    return v;
        //}

    }
}
